//
//  RootViewController.m
//  TimeTracker
//
//  Created by rodrigop on 31.03.09.
//  Copyright BD Software 2009. All rights reserved.
//

#import "RootViewController.h"
#import "TimeTrackerAppDelegate.h"
#import "Task.h"
#import "DetailViewController.h"
#import "TracDataManager.h"


@implementation RootViewController


- (void)viewDidLoad {
    [super viewDidLoad];
	NSString *user = @"rodrigop";
	NSString *pwd = @"eltach1n";
	NSString *server = @"my-trac.assembla.com";
	NSString *project = @"bd-time-tracker";

	NSString *connectionString = [NSString stringWithFormat:@"http://%@:%@@%@/%@/login/xmlrpc",user,pwd,server,project];
	if ([[TracDataManager sharedTracDataManager] checkXMLRPCTracIsRunning:connectionString]) {
		NSArray *taskList = [[TracDataManager sharedTracDataManager] tasksForUser:connectionString:user];
		NSInteger n = [taskList count];
	}
	
	
	//tasklist = [[NSMutableArray alloc] init];
	/*
	Task *newTask1 = [[[Task alloc] initWithName:@"Task1"] autorelease];
	Task *newTask2 = [[[Task alloc] initWithName:@"Task2"] autorelease];
	Task *newTask3 = [[[Task alloc] initWithName:@"Task3"] autorelease];
	Task *newTask4 = [[[Task alloc] initWithName:@"Task4"] autorelease];
	
	NSArray *completedTasksArray = [NSArray arrayWithObjects:newTask1,newTask2,nil]; //sin el nil no jala...
	NSArray *dueSoonTasksArray = [NSArray arrayWithObjects:newTask3,newTask4,nil];
	
	NSDictionary *completedTasksDict = [NSDictionary dictionaryWithObject:completedTasksArray forKey:@"Tasks"];
	NSDictionary *dueSoonTasksDict = [NSDictionary dictionaryWithObject:dueSoonTasksArray forKey:@"Tasks"];
	*/
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	
	/*
	[tasklist addObject:completedTasksDict];
	[tasklist addObject:dueSoonTasksDict];
	*/
	self.navigationItem.title=@"Task List";
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	TimeTrackerAppDelegate *appDelegate = (TimeTrackerAppDelegate *) [[UIApplication sharedApplication] delegate];
	return appDelegate.tasksFromDB.count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		CGRect CellFrame = CGRectMake(0, 0, 300, 60);
		CGRect Label1Frame = CGRectMake(10, 10, 290, 25);
		CGRect Label2Frame = CGRectMake(10, 33, 290, 25);
		UILabel *lblTemp;
		cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:CellIdentifier] autorelease];
		
		lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
		lblTemp.tag = 1;
		[cell.contentView addSubview:lblTemp];
		[lblTemp release];
		
		//Initialize Label with tag 2.
		lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
		lblTemp.tag = 2;
		lblTemp.font = [UIFont boldSystemFontOfSize:12];
		lblTemp.textColor = [UIColor lightGrayColor];
		[cell.contentView addSubview:lblTemp];
		[lblTemp release];
	
	}
	
	// Set up the cell...
	UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
	UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];

	
	//First get the dictionary object
	TimeTrackerAppDelegate *appDelegate = (TimeTrackerAppDelegate *) [[UIApplication sharedApplication] delegate];
	Task* cellValue= [appDelegate.tasksFromDB objectAtIndex:indexPath.row];
	lblTemp1.text=cellValue.taskName;
	NSDateFormatter *formater=[[[NSDateFormatter alloc] init] autorelease];
	[formater setDateStyle:NSDateFormatterShortStyle];
	lblTemp2.text=[formater stringFromDate:cellValue.dueDate];
	
	return cell;
	
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	//Get the selected task
	
	TimeTrackerAppDelegate *appDelegate = (TimeTrackerAppDelegate *) [[UIApplication sharedApplication] delegate];
	Task *selectedTask = [appDelegate.tasksFromDB objectAtIndex:indexPath.row];
	
	//Initialize the detail view controller and display it.
	DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
	dvController.selectedTask = selectedTask;
	[self.navigationController pushViewController:dvController animated:YES];
	[dvController release];
	dvController = nil;
}


- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
	
	//return UITableViewCellAccessoryDetailDisclosureButton;
	return UITableViewCellAccessoryDisclosureIndicator;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
	
	[self tableView:tableView didSelectRowAtIndexPath:indexPath];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	return 60;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
}


@end

